// var app= require('../app')
var express = require('express');
// var router = express.Router();
var dao = require('./util/dao')
var app=express()
var uuid = require('uuid')
var multer = require('multer')
var uploadTools = multer({
    storage: multer.diskStorage({ // 该存储方案将会把文件直接存入磁盘
        destination: (req, file, callback)=>{
            callback(null, 'public')
        },
        filename: (req, file, callback)=>{
            // 通过file，获取原始文件名     
            let name = file.originalname       
            // 截取源文件的后缀 .jpg  .png  ....
            let ext = name.substr(name.lastIndexOf('.'))
            // 生成一个随机文件名，调用callback返回即可
            let newName = uuid.v4() + ext
            callback(null, newName)
        }
    })
})



app.post('/upload', uploadTools.single('file'),  (req, resp)=>{
    // multer中间件将会把文件信息存入：req.files
    let url =  req.file.filename
    console.log(req.file)
    resp.send(dao.ok(url))
})

module.exports=app
